package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.ruoyi.common.core.utils.HttpUtil;
import com.ruoyi.system.domain.vo.MatchVo;
import com.ruoyi.system.domain.vo.VerifyVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.domain.DrugModel;
import com.ruoyi.system.service.IDrugModelService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 药品模型Controller
 * 
 * @author wmb
 * @date 2020-12-30
 */
@RestController
@RequestMapping("/model")
public class DrugModelController extends BaseController
{
    @Autowired
    private IDrugModelService drugModelService;

    /**
     * 查询药品模型列表
     */
    @PreAuthorize("@ss.hasPermi('system:model:list')")
    @GetMapping("/list")
    public TableDataInfo list(DrugModel drugModel)
    {
        startPage();
        List<DrugModel> list = drugModelService.selectDrugModelList(drugModel);
        return getDataTable(list);
    }

    /**
     * 导出药品模型列表
     */
    @PreAuthorize("@ss.hasPermi('system:model:export')")
    @Log(title = "药品模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DrugModel drugModel) throws IOException
    {
        List<DrugModel> list = drugModelService.selectDrugModelList(drugModel);
        ExcelUtil<DrugModel> util = new ExcelUtil<DrugModel>(DrugModel.class);
        util.exportExcel(response, list, "model");
    }

    /**
     * 获取药品模型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:model:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(drugModelService.selectDrugModelById(id));
    }

    /**
     * 新增药品模型
     */
    @PreAuthorize("@ss.hasPermi('system:model:add')")
    @Log(title = "药品模型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DrugModel drugModel)
    {
        return toAjax(drugModelService.insertDrugModel(drugModel));
    }

    /**
     * 修改药品模型
     */
    @PreAuthorize("@ss.hasPermi('system:model:edit')")
    @Log(title = "药品模型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DrugModel drugModel)
    {
        return toAjax(drugModelService.updateDrugModel(drugModel));
    }

    /**
     * 删除药品模型
     */
    @PreAuthorize("@ss.hasPermi('system:model:remove')")
    @Log(title = "药品模型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(drugModelService.deleteDrugModelByIds(ids));
    }



    @PreAuthorize("@ss.hasPermi('system:model:remove')")
    @Log(title = "药品模型", businessType = BusinessType.DELETE)
    @PostMapping("/verify")
    public AjaxResult verify(@RequestBody VerifyVo verify)
    {
        String url = "http://124.205.59.34:8001/match";
        MatchVo match = new MatchVo();

        match.setText(verify.getContent());
        match.setModel(verify.getModel());
        try {
            String result = HttpUtil.doPost(url, JSON.toJSON(match));
            JSONObject json = JSON.parseObject(result);
            return AjaxResult.success(json.getBooleanValue("result"));
        }catch (Exception e){

        }
        return AjaxResult.success();

    }


    @GetMapping(value = "/drugModelStat")
    public AjaxResult drugModelStat()
    {
        return AjaxResult.success(drugModelService.drugModelStat());
    }
}
